import 'dart:io';


void main() async {
  final server = await ServerSocket.bind('127.0.0.1', 8080);
  print('服务器已启动，监听端口 8080');

  await for (var socket in server) {
    handleClient(socket);
  }
}

void handleClient(Socket socket) {
  print('客户端连接: ${socket.remoteAddress}:${socket.remotePort}');

  socket.listen(
        (data) {
      print('接收到客户端数据: ${String.fromCharCodes(data)}');
      socket.write('服务器收到了您的消息：${String.fromCharCodes(data)}');
    },
    onError: (error) {
      print('发生错误: $error');
    },
    onDone: () {
      print('客户端断开连接');
      socket.close();
    },
  );
}
